package com.rd.teacher.jichao.day11;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class TestCallable implements Callable<String> {

    // 线程的执行逻辑写在 call()方法中
    @Override
    public String call() throws Exception {
        System.out.println(Thread.currentThread().getName()
                + " is running");
        Thread.sleep(1500);
        return "hello callable";
    }

    public static void main(String[] args) throws Exception {

        // 需要把创建的callable对象传递进future
        // 使用futureTask 来组装callable
        // 再将futureTask作为thread的构造器参数传递进去
        TestCallable tc = new TestCallable();
        FutureTask<String> task = new FutureTask<>(tc);
        Thread thread1 = new Thread(task);
        thread1.start();
        System.out.println("启动线程");

        String result = task.get();
        System.out.println("执行结果：" + result);

    }
}
